(function(){
    // Ext.data.Connection 基础类
    //   Ext.Ajax 子类，abort中断ajax请求，isLoading是否加载，
    
    function testExtAjaxRequest(){
        var request = Ext.Ajax.request({
            url: webroot + "/lesson08/data1",
            params: {id: 1},
            method: "post",
            timeout: 3000, // 毫秒
            // options 请求参数集合
            success: function(response, options){
                var json = eval(("(" + response.responseText + ")"))
                alert(json.name);
            },
            failure: function(response, options){
                alert(response.responseText);
            }
        });
        // The request object. This may be used to cancel the request.
        // Ext.Ajax.abort(request);
    }
    
    // 带form的提交
    function testExtAjaxRequest1(){
        Ext.Ajax.request({
            url: webroot + "/lesson08/form",
            params: {id: 1},
            method: "post",
            timeout: 3000,
            // 将form中值提交
            form: "myform",
            success: function(response, options){
                var json = eval(("(" + response.responseText + ")"))
                alert(json.name);
            },
            failure: function(response, options){
                alert(response.responseText);
            }
        });
    }
    
    // 带json的提交
    function testExtAjaxRequest2(){
        var data = {name: 'abc', userId: 33};
        Ext.Ajax.request({
            url: webroot + "/lesson08/json",
            headers: {"Content-Type": "application/json"}, 
            jsonData: data,
            method: "post",
            timeout: 3000,
            success: function(response, options){
                alert(response.reponseText);
            }
        });
    }
    
    // 一个联动实例，不做详细了，上面做的方法太麻烦了
    function testExtAjaxRequest3(){
        Ext.get("city").on("change", function(e, select){
//            console.dir(arguments);
//            if(data[cityName]){
//                直接取
//            }else{
//                ajax去，保存到data缓存中
//            }
            var id = select.options[select.selectedIndex].value;
            Ext.Ajax.request({
                url: webroot + "/lesson08/json",
                params: {id: id},
                method: "post",
                timeout: 3000,
                success: function(response, options){
                    // 返回数组
                    var areas = eval(response.reponseText);
                    var areaSlt = Ext.getDom("area");
                    // areaSlt.options.length = 0;
                    while(areaSlt.options.length >0){
                        areaSlt.options.remove(areaSlt.options.length -1);
                    }
                }
            });
        });
    }
    
    // Ext.ElementLoader
    function testExtElementLoader(){
        // getLoader
        var time = Ext.get("time").getLoader();
        time.load({
            url: webroot + "/lesson08/element",
            // 类似success
            renderer: function(loader, response, request){
                var time = response.responseText;
                Ext.getDom("time").value = time;
            }
        });
    }
    
    // 自动刷新，常用消息提示
    function testExtElementLoader1(){
        var time = Ext.get("time").getLoader();
        time.startAutoRefresh(2000,{
            url: webroot + "/lesson08/refresh",
            renderer: function(loader, response, request){
                var time = response.responseText;
                Ext.getDom("time").value = time;
            }
        });
    }
    
    var webroot;
    
    Ext.onReady(function(){
        webroot = Ext.get("webroot").getValue();
//        testExtAjaxRequest();
//        testExtAjaxRequest1();
//        testExtAjaxRequest2();
//        testExtAjaxRequest3();
//        testExtElementLoader();
//        testExtElementLoader1();
    });
})();